package com.cat.model.web.action.admin.order;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.AjaxObject;
import com.alonew.core.commons.page.Page;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.common.enumerator.OrderStatus;
import com.cat.model.common.enumerator.SubjectType;
import com.cat.model.common.model.common.AccountDVO;
import com.cat.model.common.model.common.AccountVO;
import com.cat.model.common.model.common.BasOrderRefundVO;
import com.cat.model.common.model.common.BasOrderVO;
import com.cat.model.common.model.common.PlmCityVO;
import com.cat.model.common.model.common.SolutionVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;
import com.cat.model.web.bean.ModelExtendtimeBean;
import com.cat.model.web.bean.SolutionModelBean;

public class ModelOrderManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;
	
	private Long id;
	private String orderNumber;
	private Integer status;
	private String subjectName;
	private Date createBeginTime;
	private Date createEndTime;
	private BigDecimal returnAmount;
	private String refundReason;
		
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private BasLogic basLogic; 
	
	@Autowired
	private TxCommonDAO txCommonDAO;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(orderNumber)){
			condition.put("orderNumber", orderNumber);
		}
		
		if(status!=null){
			condition.put("status", status);
		}
		
		if(StringUtils.isNotEmpty(subjectName)){
			condition.put("subjectName", subjectName);
		}
		
		if(createBeginTime!=null){
			condition.put("createBeginTime", createBeginTime);
		}
		
		if(createEndTime!=null){
			condition.put("createEndTime", createEndTime);
		}
		
		condition.put("subjectType", SubjectType.Model.getValue());//模特
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchBasOrderSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<BasOrderVO> list = (List<BasOrderVO>)plmCommDAO.search("manager.searchBasOrder", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String detail(){
		BasOrderVO order = modelMapper.getBasOrderByOrderNumber(orderNumber);
		request.setAttribute("item", order);
		
		if(order!=null){
			SolutionVO solution = modelMapper.getSolution(new Long(order.getRefBizId()));
			request.setAttribute("solution", solution);
			if(solution!=null){
				List<SolutionModelBean> modelList = modelMapper.getSolutionModelBySolutionId(solution.getId());
				request.setAttribute("modelList", modelList);
			}
			
			List<ModelExtendtimeBean> modelExtendList = modelMapper.getModelExtendtimeByRefOrderNo(order.getOrderNumber());
			if(modelExtendList.size()>0){
				request.setAttribute("modelExtendList", modelExtendList);
			}
		}
		
		List<PlmCityVO> cityList = plmCommDAO.getAllPlmCity();
		request.setAttribute("cityList", cityList);
		
		return "detail";
	}
	
	public String toReturnAmount(){
		return "returnAmount";
	}
	
	public void setReturnAmount(){
		BasOrderVO order = modelMapper.getBasOrderByOrderNumber(orderNumber);
		if(null == order){
			this.print(AjaxObject.newError("订单不存在，请刷新列表").setCallbackType("").toString());
			return;
		}
		
		if(!OrderStatus.REBACKING.getValue().equals(order.getStatus())){
			this.printHTML("<script>parent.callback(" + false + ", '退款中的状态的订单才能退款！')</script>");
			return;
		}
		try {
			order.setStatus(OrderStatus.REPEAL.getValue());
			txCommonDAO.updateById(order, "status");
			
			BasOrderRefundVO refund = new BasOrderRefundVO();
			refund.setOrderId(order.getId());
			refund.setOrderNumber(order.getOrderNumber());
			refund.setRefundTime(new Date());
			refund.setRefundAmount(returnAmount);
			refund.setRefundReason(refundReason);
			
			txCommonDAO.insert(refund);
			
			AccountVO account = modelMapper.getAccount(order.getUserId());
			if(account!=null){
				account.setAvailBalance(account.getAvailBalance().add(returnAmount));
				account.setBalance(account.getBalance().add(returnAmount));
				txCommonDAO.updateById(account, "availBalance","balance");
			} else {
				account = new AccountVO();
				account.setUserId(order.getUserId());
				account.setAvailBalance(returnAmount);
				account.setBalance(returnAmount);
				txCommonDAO.insert(account);
			}
			
			AccountDVO accontdD = new AccountDVO();
			accontdD.setAccountId(account.getId());
			accontdD.setAmount(returnAmount);
			accontdD.setBalance(account.getBalance());
			accontdD.setRefBizCode("02");
			accontdD.setRefBizId(refund.getId());
			accontdD.setRefBizName("退款");
			accontdD.setCreateTime(refund.getRefundTime());
			txCommonDAO.insert(accontdD);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '操作失败！')</script>");
		}
		this.printHTML("<script>parent.callback(" + true + ", '操作成功！')</script>");
	}
	
	public String orderDone() {
		modelMapper.updateStatus(id,"05");//完成
		okAlert("操作成功！");
		return "list";
	}
	
	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public String getOrderNumber() {
		return orderNumber;
	}


	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}


	public Integer getStatus() {
		return status;
	}


	public void setStatus(Integer status) {
		this.status = status;
	}


	public String getSubjectName() {
		return subjectName;
	}


	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}


	public Date getCreateBeginTime() {
		return createBeginTime;
	}


	public void setCreateBeginTime(Date createBeginTime) {
		this.createBeginTime = createBeginTime;
	}


	public Date getCreateEndTime() {
		return createEndTime;
	}


	public void setCreateEndTime(Date createEndTime) {
		this.createEndTime = createEndTime;
	}

	public BigDecimal getReturnAmount() {
		return returnAmount;
	}

	public void setReturnAmount(BigDecimal returnAmount) {
		this.returnAmount = returnAmount;
	}

	public String getRefundReason() {
		return refundReason;
	}

	public void setRefundReason(String refundReason) {
		this.refundReason = refundReason;
	}


}
